package com.ios.controller;

import com.ios.model.entity.Reservation;
import com.ios.service.ReservationService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 预约记录控制器
 */
@RestController
@RequestMapping("/reservation")
public class ReservationController extends BaseController {
    @Autowired
    private ReservationService reservationService;

    /**
     * 查询预约列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Reservation reservation) {
        startPage();
        List<Reservation> list = reservationService.selectReservationList(reservation);
        return getDataTable(list);
    }

    /**
     * 获取预约详情
     */
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(reservationService.selectReservationById(id));
    }

    /**
     * 新增预约
     */
    @PostMapping
    public AjaxResult add(@RequestBody Reservation reservation) {
        reservation.setUserId(getUserId()); // 设置当前用户ID
        return toAjax(reservationService.insertReservation(reservation));
    }

    /**
     * 修改预约
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Reservation reservation) {
        return toAjax(reservationService.updateReservation(reservation));
    }

    /**
     * 删除预约
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(reservationService.deleteReservationByIds(ids));
    }

    /**
     * 用户取消预约
     */
    @PutMapping("/cancel/{id}")
    public AjaxResult cancel(@PathVariable Long id, @RequestParam(required = false) String reason) {
        return toAjax(reservationService.cancelReservation(id, getUserId(), reason));
    }

    /**
     * 管理员审核预约
     */
    @PutMapping("/review/{id}")
    public AjaxResult review(
            @PathVariable Long id,
            @RequestParam boolean approved,
            @RequestParam(required = false) String reason
    ) {
        return toAjax(reservationService.reviewReservation(id, getUserId(), approved, reason));
    }

    /**
     * 用户签到
     */
    @PutMapping("/checkin/{id}")
    public AjaxResult checkIn(@PathVariable Long id) {
        return toAjax(reservationService.checkIn(id, getUserId()));
    }

    /**
     * 根据用户ID查询预约记录
     * @param userId 用户ID
     * @return 预约记录列表
     */
    @GetMapping("/byUser")
    public TableDataInfo getReservationsByUser(@RequestParam Long userId) {
        startPage(); // 若依分页起手式
        List<Reservation> list = reservationService.selectReservationsByUserId(userId);
        return getDataTable(list); // 自动封装分页数据
    }

    /**
     * 校验某个座位在指定时间段是否冲突
     */
    @GetMapping("/checkConflict")
    public AjaxResult checkTimeSlotConflict(Long seatId, Date reservationDate, Integer timeSlot,Long userId) {
        Reservation temp = new Reservation();
        temp.setSeatId(seatId);
        temp.setReservationDate(reservationDate);
        temp.setTimeSlot(timeSlot);
        temp.setUserId(userId);
        boolean conflict = reservationService.hasTimeSlotConflict(temp);
        return AjaxResult.success(conflict);
    }



}
